package org.timemanager.view;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.widget.RemoteViews;

/**
 * this auxiliary class is used to record current doing event,
 * and also is responsible for redraw the widget to reflect the change
 */
public class StartRecord {
	public static int curEventId=-1;
	public static boolean isAlarmRuning=false;
	/**
	 * redraw the widget
	 * @param context
	 * @param newEventId the new started event
	 * @param newEventName the new started event name
	 * @return if the specific event is the current doing one and user would like to restart it again,
	 * then there is no need to insert a new record into database,
	 * just return false to indicate such situation. 
	 */
	public static boolean startRecord(Context context,int newEventId,String newEventName){
		if(curEventId==newEventId){
			return false;
		}
		curEventId=newEventId;
		
		DatabaseTool databaseTool=new DatabaseTool(context);
		databaseTool.insertRecord(newEventId, Calendar.getInstance().getTime());
		try{
			databaseTool.close();
		}catch (Exception e) {
			// TODO: handle exception
		}
		
		RemoteViews views = new RemoteViews(context
				.getPackageName(), R.layout.time_widget);
		views.setTextViewText(R.id.cur_doing, newEventName);
		ComponentName widget = new ComponentName(context,
				TimeManagerWidget.class);
		AppWidgetManager manager = AppWidgetManager
				.getInstance(context);
		manager.updateAppWidget(widget, views);
		
		if(isAlarmRuning){
			cancelWarnAlarm(context);
		}
		setWarnAlarm(context);
		return true;
	}
	/**
	 * if the current event name was updated
	 * then the widget title should also be updated
	 */
	public static boolean updateCurDoing(Context context,int eventId){
		curEventId=eventId;
		
		DatabaseTool databaseTool=new DatabaseTool(context);		
		Cursor eventItem=databaseTool.getEvent(eventId);
		eventItem.moveToFirst();
		
		RemoteViews views = new RemoteViews(context
				.getPackageName(), R.layout.time_widget);
		views.setTextViewText(R.id.cur_doing, eventItem.getString(1));
		ComponentName widget = new ComponentName(context,
				TimeManagerWidget.class);
		AppWidgetManager manager = AppWidgetManager
				.getInstance(context);
		manager.updateAppWidget(widget, views);
		try{
			//eventItem.close();
			databaseTool.close();
		}catch (Exception e) {
			// TODO: handle exception
		}
		return true;
	}
	/**
	 * set alarm when the event has warn time
	 */
	public static void setWarnAlarm(Context context)
	{
		DatabaseTool databaseTool=new DatabaseTool(context);		
		Cursor eventItem=databaseTool.getEvent(curEventId);
		eventItem.moveToFirst();
		int warn=eventItem.getInt(4);
		try{
			//eventItem.close();
			databaseTool.close();
		}catch (Exception e) {
			// TODO: handle exception
		}
		if(warn==-1){
			return;
		}
		
		Calendar alarmTime=Calendar.getInstance();
//		Log.e("warn time",warn+"--");
        alarmTime.add(Calendar.MINUTE, warn);
//        Log.e("warn calendar",alarmTime.getTime().toString());
		
		Intent intent = new Intent(context, CallAlarmReceiver.class);
        PendingIntent sender=PendingIntent.getBroadcast(context, 0, intent, 0);
        AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);     
        am.set(AlarmManager.RTC_WAKEUP,alarmTime.getTimeInMillis(),sender);
        isAlarmRuning=true;
	}
	/**
	 * cancel alarm
	 */
	public static void cancelWarnAlarm(Context context)
	{
		Intent intent = new Intent(context, CallAlarmReceiver.class);
        PendingIntent sender=PendingIntent.getBroadcast(context, 0, intent, 0);
        AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);     
        am.cancel(sender);
        isAlarmRuning=false;
	}
}
